package com.sgb.kjwl.view.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;
import android.view.inputmethod.EditorInfo;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySreach3Binding;
import com.sgb.kjwl.di.home.DaggerHomeComponent;
import com.sgb.kjwl.di.home.HomeModule;
import com.sgb.kjwl.model.entity.RecommendEntity;
import com.sgb.kjwl.model.entity.StoreEntity;
import com.sgb.kjwl.view.ui.activity.manage.ShopInfoForCompanyTradesActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopGoodsDetailActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopInfoActivity;
import com.sgb.kjwl.view.ui.adapter.home.SearchGoodsAdapter;
import com.sgb.kjwl.view.ui.adapter.home.SearchStoreAdapter3;
import com.sgb.kjwl.viewmodel.home.SearchModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * Description:
 * Author CFK
 * Time
 */
public class SearchActivity3 extends AppActivity {
    private ActivitySreach3Binding binding;
    private LinearLayoutManager linearLayoutManager;
    private SearchStoreAdapter3 searchStoreAdapter3;
    private SearchGoodsAdapter searchGoodsAdapter;
    private int type;
    private boolean isGoods;
    private int status;
    @Inject
    SearchModel3 viewModel;

    public static void start(Context context, int type) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.setClass(context, SearchActivity3.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerHomeComponent.builder().appComponent(appComponent).homeModule(new HomeModule(this)).build().inject(this);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        isGoods = true;
        type = getIntent().getIntExtra("type", 0);
        binding = DataBindingUtil.setContentView(SearchActivity3.this, R.layout.activity_sreach3);
        binding.include.ivBack.setOnClickListener(this);
        binding.tvSearch.setOnClickListener(this);
        binding.tvGoods.setOnClickListener(this);
        binding.tvShop.setOnClickListener(this);
        binding.include.titleView.setText("搜索");
        linearLayoutManager = new LinearLayoutManager(this);
        searchStoreAdapter3 = new SearchStoreAdapter3(this, null);
        searchGoodsAdapter = new SearchGoodsAdapter(this, null);
        searchStoreAdapter3.setOnItemClick(new AdapterOnItemClick<StoreEntity>() {
            @Override
            public void onItemClick(StoreEntity storeEntity, int position) {
                ShopInfoForCompanyTradesActivity.startAct(getActivity(), storeEntity.getId(), "", 0, 0);

            }
        });
        searchGoodsAdapter.setOnItemClick(new AdapterOnItemClick<RecommendEntity>() {
            @Override
            public void onItemClick(RecommendEntity recommendEntity, int position) {
                ShopGoodsDetailActivity.start(getActivity(), recommendEntity.getId(), "", recommendEntity.getSkuid());
            }
        });
        binding.rv.setLayoutManager(linearLayoutManager);
        binding.rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (isGoods) {
                    viewModel.goodsSearch(binding.etView.getText().toString());
                } else {
                    viewModel.storeSearch(binding.etView.getText().toString());
                }
            }

            @Override
            public void onLoadMore() {
                if (isGoods) {
                    viewModel.goodsSearchMore(binding.etView.getText().toString());
                } else {
                    viewModel.storeSearchMore(binding.etView.getText().toString());
                }
            }
        });

        binding.etView.setOnEditorActionListener((v, actionId, event) -> {
            binding.llAll.setVisibility(View.GONE);
            status = 1;
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (isGoods) {
                    binding.rv.setAdapter(searchGoodsAdapter);
                    viewModel.goodsSearch(binding.etView.getText().toString().trim());
                } else {
                    binding.rv.setAdapter(searchStoreAdapter3);
                    viewModel.storeSearch(binding.etView.getText().toString().trim());
                }
                return true;
            }
            return false;
        });
    }


    @Override
    protected void init() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                if (status == 1) {
                    status = 0;
                    binding.llAll.setVisibility(View.VISIBLE);
                    binding.emptyView.setVisibility(View.GONE);
                    binding.rv.setVisibility(View.GONE);
                } else {
                    finish();
                }
                break;
            case R.id.tv_search:
                status = 1;
                if (isGoods) {
                    binding.rv.setAdapter(searchGoodsAdapter);
                    viewModel.goodsSearch(binding.etView.getText().toString());
                } else {
                    binding.rv.setAdapter(searchStoreAdapter3);
                    viewModel.storeSearch(binding.etView.getText().toString());
                }
                break;
            case R.id.tv_shop:
                status = 1;
//                binding.rv.setAdapter(searchStoreAdapter3);
               // viewModel.storeSearch(binding.etView.getText().toString());
                isGoods = false;
                binding.etView.setHint("搜索店铺");
                break;
            case R.id.tv_goods:
                status = 1;
//                binding.rv.setAdapter(searchGoodsAdapter);
                binding.etView.setHint("搜索商品");
            //    viewModel.goodsSearch(binding.etView.getText().toString());
                isGoods = true;
                break;
        }
    }

    public void refresh(List<RecommendEntity> recommendEntityList, List<StoreEntity> storeEntityList, boolean isLast) {
        binding.llAll.setVisibility(View.GONE);
        binding.emptyView.setVisibility(View.GONE);
        binding.rv.setVisibility(View.VISIBLE);
        binding.rv.setLoadingMoreEnabled(true);
        if (isGoods) {
            searchGoodsAdapter.setDatas(recommendEntityList);
        } else {
            searchStoreAdapter3.setDatas(storeEntityList);
        }
        binding.rv.refreshComplete();
        if (isLast) {
            binding.rv.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.rv.refreshComplete();
//        binding.rlvView.setVisibility(View.GONE);
        if (isGoods) {
            searchGoodsAdapter.setDatas(null);
        } else {
            searchStoreAdapter3.setDatas(null);
        }
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    public void loadMore(List<RecommendEntity> recommendEntityList, List<StoreEntity> storeEntityList, boolean isLast) {
        if (isGoods) {
            searchGoodsAdapter.addDatas(recommendEntityList);
        } else {
            searchStoreAdapter3.addDatas(storeEntityList);
        }
        binding.rv.loadMoreComplete();
        if (isLast) {
            binding.rv.setLoadingMoreEnabled(false);
        }
    }

    @Override
    public void onBackPressed() {
        if (status == 1) {
            status = 0;
            binding.llAll.setVisibility(View.VISIBLE);
            binding.emptyView.setVisibility(View.GONE);
            binding.rv.setVisibility(View.GONE);
        } else {
            finish();
        }
    }
}
